<template>
	<view v-if="goods_info.goods_name" class="goods-detail-container">
		<!-- 商品轮播图区域 -->
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
			<swiper-item v-for="(item,i) in goods_info.pics" :key="i" @click="preview(i)">
				<image :src="item.pics_big"></image>
			</swiper-item>
		</swiper>
		<!-- 商品信息区域 -->
		<view class="goods-info-box">
			<view class="price">￥ {{ goods_info.goods_price }}</view>
			<view class="goods-info-body">
				<view class="goods-name">{{ goods_info.goods_name }}</view>
				<view class="favi">
					<uni-icons type="star" size="20" color="gray"></uni-icons>
					<text>收藏</text>
				</view>
			</view>
			<view class="goods-footer">快递：免运费</view>
		</view>

		<!-- 商品详情信息 -->
		<rich-text :nodes="goods_info.goods_introduce"></rich-text>

		<!-- 底部商品导航组件区域 -->
		<view class="goods-carts">
			<uni-goods-nav :options="options" :fill="true" :button-group="buttonGroup" @click="onClick"
				@buttonClick="buttonClick" />
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations,
		mapGetters
	} from 'vuex'
	export default {
		data() {
			return {
				// 商品详情对象
				goods_info: {},
				options: [{
					icon: 'shop',
					text: '店铺',
					infoBackgroundColor: '#007aff',
					infoColor: "#f5f5f5"
				}, {
					icon: 'cart',
					text: '购物车',
					info: 0
				}],
				buttonGroup: [{
						text: '加入购物车',
						backgroundColor: 'linear-gradient(90deg, #FFCD1E, #FF8A18)',
						color: '#fff'
					},
					{
						text: '立即购买',
						backgroundColor: 'linear-gradient(90deg, #FE6035, #EF1224)',
						color: '#fff'
					}
				]
			}
		},
		onLoad(options) {
			const goods_id = parseInt(options.goods_id)
			this.getGoodsIDetail(goods_id)
		},
		computed: {
			...mapState('m_cart', []),
			...mapGetters('m_cart', ['total'])
		},
		watch: {
			// 监听购物车商品总数量的变化 页面加载完毕不会马上执行
			// total(newValue){
			// 	// 找到购物车的配置对象 
			// 	const findResult = this.options.find(item => item.text == '购物车')
			// 	if(findResult) {
			// 		// 为购物车按钮上 info 动态赋值
			// 		findResult.info = newValue
			// 	}
			// }

			// 数据加载完毕 totaal立马执行 购物车的info会马上渲染
			total: {
				handler(newValue) {
					// 找到购物车的配置对象
					const findResult = this.options.find(item => item.text === '购物车')
					if (findResult) {
						// 为购物车按钮上 info 动态赋值
						findResult.info = newValue
					}
				},
				immediate: true
			}
		},
		methods: {
			...mapMutations('m_cart', ['addToCart']),
			// 接口/api/public/v1/goods/detail
			async getGoodsIDetail(goods_id) {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/detail', {
					goods_id
				})
				if (res.meta.status !== 200) return uni.$showMsg()

				// 解决页面中富文本展示区域的图片底部有空白间隙的问题，使用replace()方法 为·img标签添加行内样式
				// g 指的是全局替换
				res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g,
					'<img style="display:block;" ').replace(/webp/g, 'jpg')
				this.goods_info = res.message
			},
			// 实现轮播图的预览效果
			preview(i) {
				// 调用 uni.previewImage 方法预览图片
				uni.previewImage({
					// 从哪一项开始预览
					current: i,
					urls: this.goods_info.pics.map(item => item.pics_big)
				})
			},
			onClick(e) {
				if (e.content.text === '购物车') {
					uni.switchTab({
						url: '/pages/Cart/Cart'
					})
				}
			},
			buttonClick(e) {
				if (e.content.text === '加入购物车') {
					// 定义商品的信息队对象
					const goods = {
						// 商品ID
						goods_id: this.goods_info.goods_id,
						// 商品的商品名称
						goods_name: this.goods_info.goods_name,
						// 商品的价格
						goods_price: this.goods_info.goods_price,
						// 商品的数量
						goods_count: 1,
						// 商品的图片
						goods_small_logo: this.goods_info.goods_small_logo,
						// 商品的勾选状态
						goods_state: true
					}

					// 通过this 调用映射过来的 addToCart 方法,把信息存储到购物车中
					this.addToCart(goods)
				}
			}
		}
	}
</script>

<style lang="scss">
	.goods-detail-container {
		padding-bottom: 50px;
	}

	swiper {
		height: 750rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.goods-info-box {
		padding: 10px;
		padding-right: 0;

		.price {
			color: #C00000;
			font-size: 18px;
			margin: 10px 0;
		}

		.goods-info-body {
			display: flex;
			justify-content: space-between;

			.goods-name {
				font-size: 13px;
				margin-right: 10px;
			}

			.favi {
				width: 120px;
				color: gray;
				font-size: 12px;
				border-left: 2px solid #efefef;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
			}
		}

		.goods-footer {
			color: #999999;
			font-size: 12px;
			margin: 10px 0;
		}
	}

	.goods-carts {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		z-index: 999;
	}
</style>